package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.ColdDirectReq;
import com.ruibang.glass.quality.domain.resp.ProportionInfo;
import com.ruibang.glass.quality.entity.EverydayColdDirectLoss;
import com.ruibang.glass.quality.mapper.EverydayColdDirectLossMapper;
import com.ruibang.glass.quality.service.EverydayColdDirectService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class EverydayColdDirectServiceImpl extends ServiceImpl<EverydayColdDirectLossMapper, EverydayColdDirectLoss> implements EverydayColdDirectService {

    @Override
    public ResultBody queryEverydayColdDirect(ColdDirectReq coldDirectReq) {
        final IPage<EverydayColdDirectLoss> page = new Page<>(coldDirectReq.getPageNo(), coldDirectReq.getPageSize());
        return ResultBody.success(baseMapper.queryEverydayColdDirect(page, coldDirectReq));
    }

    @Override
    public List<EverydayColdDirectLoss> queryTodayData(String today) {
        return baseMapper.queryTodayData(today);
    }

    @Override
    public ResultBody queryCheckAverage(@RequestBody ColdDirectReq coldDirectReq) {
        return ResultBody.success(baseMapper.queryCheckAverage(coldDirectReq));
    }

    @Override
    public ResultBody queryDefect(@RequestBody ColdDirectReq coldDirectReq) {
        List<EverydayColdDirectLoss> directLosses = baseMapper.queryDefect(coldDirectReq);
        if (CollectionUtils.isNotEmpty(directLosses)) {
            long platinum = 0;
            long stone = 0;
            long bubble = 0;
            for (EverydayColdDirectLoss directLoss : directLosses) {
                platinum = platinum + directLoss.getPlatinumTotalNum();
                stone = stone + directLoss.getStoneTotalNum();
                bubble = bubble + directLoss.getBubbleTotalNum();
            }

            ProportionInfo info = new ProportionInfo();
            info.setPlatinum(platinum);
            info.setStone(stone);
            info.setBubble(bubble);
            return ResultBody.success(info);
        }
        return ResultBody.success();
    }


    /**
     * @Description: 根据时间获取数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-15 11:22
     */
    @Override
    public List<EverydayColdDirectLoss> getByDate(ArrayList<String> date) {
        QueryWrapper<EverydayColdDirectLoss> hlQw = new QueryWrapper<>();
        hlQw.lambda().in(EverydayColdDirectLoss::getDirectDate, date);
        return this.list(hlQw);
    }
}
